package org.lq.ht.view.vip;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;

import org.lq.ht.entity.Dictionary;
import org.lq.ht.entity.TypeID;
import org.lq.ht.service.DictionaryService;
import org.lq.ht.service.impl.DictionaryServiceImpl;
import org.lq.ht.view.dictionary.V_dictionary;

import javax.swing.JTree;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JPopupMenu;
import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

import javax.swing.JMenuItem;
import javax.swing.JOptionPane;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class V_VipType extends JFrame {

	private JPanel contentPane;
	private static JTree tree;
	private JButton btnNewButton;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					V_VipType frame = new V_VipType();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public V_VipType() {
		setTitle("Vip类型");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 261, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);

		JPanel panel = new JPanel();
		contentPane.add(panel, BorderLayout.SOUTH);

		btnNewButton = new JButton("添加");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//添加
				String value = JOptionPane.showInputDialog(V_VipType.this, "请输入会员的级别名称" );
				if(value == null || value.equals("") ) {
					System.out.println("未输入");
				}else {
					DictionaryService ds = new DictionaryServiceImpl();
					Dictionary d= new Dictionary();
					d.setName(value);
					d.setType_id(TypeID.会员级别.getId());
					if(ds.saveDictionary(d)) {
						initTree();
					}
				}
			}
		});
		panel.add(btnNewButton);

		JScrollPane scrollPane = new JScrollPane();
		contentPane.add(scrollPane, BorderLayout.CENTER);

		tree = new JTree();
		scrollPane.setViewportView(tree);

		JPopupMenu popupMenu = new JPopupMenu();
		addPopup(tree, popupMenu);

		JMenuItem mntmNewMenuItem = new JMenuItem("修改");
		mntmNewMenuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//修改
				//获得鼠标点击的值
				DefaultMutableTreeNode dmtn = (DefaultMutableTreeNode) tree.getSelectionPath().getLastPathComponent();
				Dictionary d = (Dictionary) dmtn.getUserObject();
				String value = JOptionPane.showInputDialog(V_VipType.this,"请输入新的会员级别");
				if(value == null || value.equals("")) {
					System.out.println("未输入");
				}else {
					d.setName(value);
					DictionaryService ds = new DictionaryServiceImpl();
					if(ds.updateDictionary(d)) {
						initTree();
					}
				}
			}
		});
		popupMenu.add(mntmNewMenuItem);

		JMenuItem mntmNewMenuItem_1 = new JMenuItem("删除");
		mntmNewMenuItem_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//通过节点的数据模型,删除选中的节点对象
				TreePath selectionPath = tree.getSelectionPath();
				if(selectionPath!=null) {
					//删除
					DefaultMutableTreeNode dmtn = (DefaultMutableTreeNode) tree.getSelectionPath().getLastPathComponent();
					DictionaryService ds = new DictionaryServiceImpl();
					Dictionary d = (Dictionary) dmtn.getUserObject();
					String name = d.getName();
					int n = JOptionPane.showConfirmDialog(V_VipType.this, "确定删除吗?");
					if(n==0) {
						List vipType = ds.isVipType();

						boolean vipTypeBool = false;
						for (Object object : vipType){
							if (((ds.getById((int)object)).getName()).equals(name)) {
								System.out.println("商品类型名称:"+(ds.getById((int)object).getName()));
								vipTypeBool = true;
								break;
							}else {
								vipTypeBool = false;
							}
						}

						if(vipTypeBool==false) {
							//删除节点
							if(ds.deleteDictionaryById(d.getNo())) {
								JOptionPane.showMessageDialog(V_VipType.this, "删除成功!");
								initTree();
							};
						}else {
							JOptionPane.showMessageDialog(V_VipType.this, "类型正在使用!");
						}
					}
				}else {
					//未选中节点
					JOptionPane.showMessageDialog(V_VipType.this, "请选择节点后删除!");
				}

			}
		});
		popupMenu.add(mntmNewMenuItem_1);

		initTree();
	}

	public void initTree(){
		DictionaryService ds = new DictionaryServiceImpl();
		List<Dictionary> list = ds.findById(TypeID.会员级别);

		DefaultMutableTreeNode dmtn = new DefaultMutableTreeNode("会员级别");
		for (Dictionary d : list) {
			dmtn.add(new DefaultMutableTreeNode(d));
		}
		DefaultTreeModel dtm = new DefaultTreeModel(dmtn);
		tree.setModel(dtm);
	}

	private static void addPopup(Component component, final JPopupMenu popup) {
		component.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			public void mouseReleased(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			private void showMenu(MouseEvent e) {
				int index = tree.getRowForLocation(e.getX(), e.getY());
				if(index>0) {
					tree.setSelectionRow(index);
					popup.show(e.getComponent(), e.getX()+20, e.getY()+10);
				}
			}
		});
	}
}
